import os
import random
import argparse
from moviepy.editor import *
from datetime import datetime
import time

test_start_time = time.time()  # 获取当前时间戳（开始时间）
print(f"开始: {test_start_time}") 

def get_file_name_without_extension(file_path):
    # 获取文件名和扩展名
    file_name, _ = os.path.splitext(os.path.basename(file_path))
    # 返回不带扩展名的文件名
    return file_name

# 替换以下占位符为实际文件夹路径
def is_empty_directory(directory_path):
    if not os.path.exists(directory_path):
        raise FileNotFoundError(f"目录不存在：{directory_path}")

    if not os.path.isdir(directory_path):
        raise ValueError(f"给定的路径不是目录：{directory_path}")

    return len(os.listdir(directory_path)) == 0

# 解析命令行参数
parser = argparse.ArgumentParser(description="从指定目录中随机挑选片段，组合成指定长度的视频片段。")
parser.add_argument("-iv", "--video_path", type=str, default="input", help="视频文件路径，默认为空。")
parser.add_argument("-ov", "--output_video_path", type=str, default="clips_video", help="输出文件路径，默认为空。")
parser.add_argument("-oa", "--output_audio_path", type=str, default="clips_audio", help="输出文件路径，默认为空。")
parser.add_argument("-min", "--target_min_duration", type=int, default=1, help="最小输出片段市场，默认及最小1秒。")
parser.add_argument("-max", "--target_max_duration", type=int, default=3, help="最大输出片段市场，默认3秒。")
parser.add_argument("-wa", "--output_video_with_audio", type=int,default=1, help="输出视频是否带上原音频。")
parser.add_argument("-ev", "--output_video_enable", type=int,default=1, help="是否输出片段视频。")
parser.add_argument("-ea", "--output_audio_enable", type=int,default=0, help="是否输出片段音频。")
parser.add_argument("-eta", "--output_total_audio_enable", type=int,default=0, help="输出输出完整音频。")
parser.add_argument("-vw", "--width", type=int, default=720, help="输出视频宽度，默认为1280像素。")
parser.add_argument("-vh", "--height", type=int, default=1280, help="输出视频高度，默认为720像素。")
parser.add_argument("-qv", "--quality", type=int, default=3, help="视频质量，默认为3。")
args = parser.parse_args()

# 替换以下占位符为实际文件夹路径
input_directory = args.video_path
output_video_directory = args.output_video_path
output_audio_directory = args.output_audio_path

# 确保文件夹路径有效
if not os.path.exists(input_directory):
    raise FileNotFoundError(f"视频文件夹不存在：{input_directory}")

if is_empty_directory(input_directory):
    raise ValueError(f"视频文件夹为空：{input_directory}")

if not os.path.exists(output_video_directory):
    os.makedirs(output_video_directory)

if not os.path.exists(output_audio_directory):
    os.makedirs(output_audio_directory)

# 获取目录中的所有视频文件
video_files = [f for f in os.listdir(input_directory) if f.endswith(('.mp4', '.avi', '.mkv'))]

# 设置preset值以控制编码速度和压缩效果
preset_values=["ultrafast","superfast","veryfast","faster","fast","medium","slow","slower","veryslow"]
preset_index=args.quality
if preset_index<0 or preset_index>=len(preset_values):
    preset_index=len(preset_values)-1
preset_value=preset_values[preset_index]
print("视频质量：",preset_value)
t_min=args.target_min_duration
if t_min<1:
   t_min=1
t_max=args.target_max_duration   
print("片段最小时长",t_min)
print("片段最大时长",t_max)

# 切割视频并分别保存视频和音频
for video_file in video_files:
    video_path = os.path.join(input_directory, video_file)
    video_clip = VideoFileClip(video_path)
    audio_clip = video_clip.audio

    file_name= get_file_name_without_extension(video_path)

    if args.output_total_audio_enable==1 and audio_clip is not None:
        # 是否输出完整音频
        current_time = datetime.now().strftime("%Y%m%d%H%M%S")
        output_audio_filename = f"{file_name}_total_{current_time}_{int(video_clip.duration)}s.mp3"
        audio_output_path = os.path.join(output_audio_directory, f"{output_audio_filename}")
        audio_clip.write_audiofile(audio_output_path, codec='libmp3lame')

    # 切割视频和音频
    video_duration=int(video_clip.duration)
    cur_total_duration=0
    while cur_total_duration<video_duration:
        current_time = datetime.now().strftime("%Y%m%d%H%M%S")

        # 随机选择一个片段时长
        target_duration = random.randint(t_min, t_max)
        if target_duration<t_min:
            # 最后一段不足最小值，跳出
            break


        start_time = cur_total_duration
        end_time = start_time+target_duration
        end_time = min(end_time,video_duration)

        cur_total_duration+=target_duration
        
        # 切割音频
        if audio_clip is not None:
            audio_segment = audio_clip.subclip(start_time, end_time)
            if args.output_audio_enable==1:
                # 是否输出音频
                output_audio_filename = f"{file_name}_{current_time}_{target_duration}s.mp3"
                audio_output_path = os.path.join(output_audio_directory, f"{output_audio_filename}")
                audio_segment.write_audiofile(audio_output_path, codec='libmp3lame')


        if args.output_video_enable==1:
            # 是否输出视频片段
            # 切割视频
            video_segment = video_clip.subclip(start_time, end_time).without_audio()
            # 将音频片段添加到视频片段中
            if args.output_video_with_audio==1 and audio_segment is not None:
                video_segment = video_segment.set_audio(audio_segment)
             # 调整视频宽度和高度
            video_segment = video_segment.resize((args.width, args.height))
            output_video_filename = f"{file_name}_{current_time}_{target_duration}s.mp4"
            video_output_path = os.path.join(output_video_directory, f"{output_video_filename}")
            video_segment.write_videofile(video_output_path, codec='libx264',preset=preset_value)

        audio_segment=None
        print(f"已切割: {video_path}，片段: {start_time}-{end_time}")


   


test_end_time = time.time()      # 获取当前时间戳（结束时间）

elapsed_time = test_end_time - test_start_time  # 计算执行时间

print(f"结束: {test_end_time}") 
print(f"执行时间: {elapsed_time:.2f} 秒")  # 打印执行时间，保留两位小数